/*
 * File      : application.c
 * This file is part of RT-Thread RTOS
 * COPYRIGHT (C) 2006, RT-Thread Development Team
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rt-thread.org/license/LICENSE
 *
 * Change Logs:
 * Date           Author       Notes
 * 2009-01-05     Bernard      the first version
 */

/**
 * @addtogroup STM32
 */
/*@{*/

#include <stdio.h>
#include <rtthread.h>
#include <board.h>

#ifdef RT_USING_DFS
#include <dfs_fs.h>
#endif

#ifdef RT_USING_USB_HOST
extern void rt_hw_susb_init(void);
#endif

#ifdef RT_USING_COMPONENTS_INIT
extern rt_components_init(void);
#endif

#ifdef RT_USING_RTGUI
extern geoui_init(void);
#endif


void rt_platform_init(void)
{
// FINSH
	finsh_set_device("uart1");
// SPI
#ifdef RT_USING_SPI

#endif /* RT_USING_SPI */

// USB HOST
#ifdef RT_USING_USB_HOST
    /* register stm32 usb host controller driver */
    rt_hw_susb_init();
#endif /* RT_USING_USB_HOST */

// USB DEVICE
#ifdef RT_USING_USB_DEVICE
    /* usb device controller driver initilize */
    rt_hw_usbd_init();

    rt_usb_device_init("usbd");
#endif /* RT_USING_USB_DEVICE */

// FS
#ifdef RT_USING_DFS
    /* initilize sd card */
	#ifdef RT_USING_SDIO
    rt_mmcsd_core_init();
    rt_mmcsd_blk_init();
    stm32f4xx_sdio_init();
    rt_thread_delay(RT_TICK_PER_SECOND);
	#else
    rt_hw_sdcard_init();
	#endif
#endif /* RT_USING_DFS */

// GUI
#ifdef RT_USING_RTGUI
	
    rtgui_system_server_init();

#endif /* RT_USING_RTGUI */

#ifdef RT_USING_RTC
    rt_hw_rtc_init();
#endif /* RT_USING_RTC */

    rt_thread_delay(50);
    rt_device_init_all();
}
void rt_init_thread_entry(void* parameter)
{
#ifdef RT_USING_COMPONENTS_INIT
    /* initialization RT-Thread Components */
    rt_components_init();
#endif
    rt_platform_init();
	

#ifdef RT_USING_DFS
	if (dfs_mount("sd0", "/", "elm", 0, 0) == 0)
	{
		rt_kprintf("sd0 mount to / \n");
	}
	else
	{
		rt_kprintf("sd0 mount to / failed!, fatmat and try again!\n");
		/* fatmat filesystem. */
		dfs_mkfs("elm", "sd0");

		/* re-try mount. */
		if (dfs_mount("sd0", "/", "elm", 0, 0) == 0)
		{
			rt_kprintf("sd0 mount to / \n");
		}
		else
		{
			rt_kprintf("sd0 mount to / failed!\n");
		}
		
	}	
#endif /* RT_USING_DFS */
#ifdef RT_USING_RTGUI
	

	geoui_init();	
#endif /* RT_USING_RTGUI */

}

int rt_application_init(void)
{
    rt_thread_t tid;

    tid = rt_thread_create("init",
                           rt_init_thread_entry, RT_NULL,
                           2048, RT_THREAD_PRIORITY_MAX/3, 20);
    if (tid != RT_NULL)
        rt_thread_startup(tid);

    return 0;
}

/*@}*/
